<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolesAndPermissionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->unique()->comment('名称');
            $table->string('description')->nullable()->comment('描述');
            $table->boolean('status')->default(1)->comment('状态：1 => 启用，0 => 禁用');
            $table->timestamps();
        });

        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('pid')->default(0)->comment('所属分类');
            $table->string('title', 50)->comment('名称');
            $table->string('name', 50)->unique()->comment('标识');
            $table->boolean('status')->default(1)->comment('状态：1 => 启用，0 => 禁用');
            $table->boolean('type')->default(1)->comment('类型：1 => 菜单，0 => 权限');
            $table->unsignedSmallInteger('sort')->default(200)->comment('排序');
            $table->string('icon', 50)->nullable()->comment('菜单标识');
            $table->timestamps();
        });

        Schema::create('role_users', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->default(0);
            $table->unsignedInteger('role_id')->default(0);
        });

        Schema::create('permission_roles', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('role_id')->default(0);
            $table->unsignedInteger('permission_id')->default(0);
        });

        Schema::create('permission_users', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->default(0);
            $table->unsignedInteger('permission_id')->default(0);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('roles');
        Schema::dropIfExists('permissions');
        Schema::dropIfExists('role_users');
        Schema::dropIfExists('permission_roles');
        Schema::dropIfExists('permission_users');
    }
}
